<?php
namespace Home\Controller;
use Think\Controller;
class LxwmController extends IndexController{
	public function index(){
		$m_cate = M('Cate');
        $m_article = M('Article');
        //顶级栏目信息
        $top = $m_cate->field(array('id','name','contro_name'))->where(array('contro_name'=>CONTROLLER_NAME))->find();
        //栏目列表
        $cate_list = $m_cate->where(array('parent_id'=>$top['id']))->order('sort ASC')->select();
        $this->assign('top',$top);
        $this->assign('cate_list',$cate_list);
        //内容列表
        $id = I('get.id');
        if(!empty($id)){
            $cate_name = M('Cate')->where(array('id'=>$id))->find();
            $this->assign('cate_id',$id);
            $this->assign('cate_name',$cate_name['name']);
            //判断栏目类型
            switch ($cate_name['type']) {
                case '1':
                    $this->lst(array($id),C('NEWS_PAGE'));
                    $this->setStyle('new');
                    $this->display('Index/news');
                    break;
                case '2':
                    $data = $m_cate->where(array('id'=>$id))->find();
                    $this->assign('data',$data);
                    $this->setStyle('about');
                    $this->display('Index/danye');
                    break;
                case '3':
                    $this->lst(array($id),C('PIC_PAGE'));
                    $this->setStyle('pro');
                    $this->display('Index/pic');
                    break;
                case '-1':
                    $this->setStyle('contact');
                    $this->display('Index/lxwm');
                    break;
                default:
                    $this->lst(array($id),C('NEWS_PAGE'));
                    $this->setStyle('new');
                    $this->display('Index/news');
                    break;
            }
        }
        else{
            //判断第一个栏目类型
            if($cate_list[0]['type'] == -1){
                $this->setStyle('contact');
                $this->display('Index/lxwm');
            }
            else if($cate_list[0]['type'] != 2){
                foreach($cate_list as $val){
                    $cate_id[] = $val['id'];
                }
                switch ($cate_list[0]['type']) {
                    case '1':
                        $this->lst($cate_id,C('NEWS_PAGE'));
                        $this->setStyle('new');
                        $this->display('Index/news');
                        break;
                    case '3':
                        $this->lst($cate_id,C('PIC_PAGE'));
                        $this->setStyle('pro');
                        $this->display('Index/pic');
                        break;
                    // case '4':
                    //     $this->lst($cate_id,C('NEWS_PIC_PAGE'));
                    //     $this->display('Index/news');
                    //     break;
                    default:
                        $this->lst($cate_id,C('NEWS_PAGE'));
                        $this->setStyle('new');
                        $this->display('Index/news');
                        break;
                }
            }
            else{
                $data = $m_cate->where(array('id'=>$cate_list[0]['id']))->find();
                $this->assign('data',$data);
                $this->setStyle('about');
                $this->display('Index/danye');
            }
        }
	}

	public function lianxi(){
		$data['name'] = I('post.name');
		$data['phone'] = I('post.phone');
        $data['email'] = I('post.email');
        $data['qq'] = I('post.qq');
		$data['content'] = I('post.content');
		$data['ctime'] = time();
        $check = I('post.check');
        // $verify = new \Think\Verify();    
        // if(!$verify->check($check)){
        //     $this->ajaxReturn('验证码错误');
        // }

		$validate = array(
			array('name','require','请填写姓名','1'),
			array('phone','require','请填写手机号','1'),
			array('phone','number','手机号只能为数字','1'),
            array('email','require','请填写电话','1'),
            array('email','email','邮箱格式不正确','1'),
            array('qq','number','QQ号只能为数字'),
		);
		$m_msg = M('Msg');
		if($m_msg->validate($validate)->create($data)){
			if($m_msg->add()){
				$this->ajaxReturn('OK');
			}
			else{
				$this->ajaxReturn('留言失败');
			}
		}
		else{
			$this->ajaxReturn($m_msg->getError());
		}
	}
}